import hashlib

from django.db import models
# Create your models here.
from django.utils import timezone

from project_manage.libs.storage.fields import ImageUploadFKField, TimeRangField


def get_md5(name):  # 把任意长度的数据转换为一个长度固定的数据串
    if isinstance(name, str):
        name = name.encode("utf-8")
    m = hashlib.md5()
    m.update(name)


def before_defect_image_name(instance, filename):
    if isinstance(filename, str):  # 判断name是否是str类型的一个实例
        split = filename.split('.')
        file_rename = '{}.{}'.format(
            get_md5(instance.name + split[0]), split[1])  # 对文件名进行重编码，并规定文件读写路径
        pic_write_path = 'BeforeProjectDefectPost/' + file_rename
        return pic_write_path


def after_defect_image_name(instance, filename):
    if isinstance(filename, str):  # 判断name是否是str类型的一个实例
        split = filename.split('.')
        file_rename = '{}.{}'.format(
            get_md5(instance.name + split[0]), split[1])  # 对文件名进行重编码，并规定文件读写路径
        pic_write_path = 'AfterProjectDefectPost/' + file_rename
        return pic_write_path


class ProjectModel(models.Model):
    """项目表"""

    name = models.CharField('项目名称', max_length=100)
    description = models.TextField('项目描述', max_length=100)
    testers = models.ManyToManyField('core_auth.PersonModel', 'test_project')
    developer = models.ManyToManyField('core_auth.PersonModel', 'dev_project')
    start_date = models.DateField('开始时间', null=True, blank=True)
    end_date = models.DateField('结束时间', null=True, blank=True)

    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    is_delete = models.BooleanField('是否删除', default=False)

    class Meta:
        verbose_name = "项目列表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class ProjectDefectPostModel(models.Model):
    """项目缺陷报告表"""
    defect_level_choices = (
        (1, 'UI缺陷'),
        (2, '一般缺陷'),
        (3, '致命缺陷'),
    )
    status_choices = (
        (0, '已关闭'),
        (1, '已发起'),
        (2, '已响应'),
        (3, '已完成'),
    )
    title = models.CharField('缺陷标题', max_length=100)
    description = models.TextField('缺陷描述', max_length=100)
    defect_level = models.IntegerField('缺陷等级', choices=defect_level_choices)
    before_defect_image = ImageUploadFKField(null=True, blank=True, verbose_name='差异图', related_name='pdp_bdi')
    after_defect_image = ImageUploadFKField(null=True, blank=True, verbose_name='修复图', related_name='pdp_adi')

    creator = models.ForeignKey('core_auth.PersonModel', models.CASCADE, 'defect_post', verbose_name='创建者')
    project = models.ForeignKey('project.ProjectModel', models.CASCADE, 'project_defect', verbose_name='所属项目')

    release_time = models.DateTimeField('发起时间', default=timezone.now)
    expected_resolution_time = models.DateTimeField('预期解决时间', null=True, blank=True)

    response_time = models.DateTimeField('响应时间', null=True, blank=True)
    actual_resolution_time = models.DateTimeField('实际解决时间', null=True, blank=True)

    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    status = models.IntegerField('状态', choices=status_choices, default=1)
    is_delete = models.BooleanField('是否删除', default=False)

    class Meta:
        verbose_name = "缺陷报告"
        verbose_name_plural = verbose_name
